<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/11 0011
 * Time: 17:35
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "problem_list";
//常见问题列表
if($op=='problem_list'){
    $condition=array('uniacid'=>$uniacid);
    $all=pdo_getall('cqkundian_farm_problem',$condition);
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page'] :1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_problem',$condition,'','','rank asc',array($pageIndex,$pageSize));
    for ($i=0;$i<count($list);$i++){
        $typeName=pdo_get('cqkundian_farm_problem_type',array('id'=>$list[$i]['type_id'],'uniacid'=>$uniacid));
        $list[$i]['type_name']=$typeName['type_name'];
    }
    include $this->template('web/problem/problem_list');
}

//常见问题编辑
if($op=='problem_list_edit'){
    $typeData=pdo_getall('cqkundian_farm_problem_type',array('uniacid'=>$uniacid,'pid'=>0));
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_problem',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $childType=pdo_get('cqkundian_farm_problem_type',array('uniacid'=>$uniacid,'id'=>$list['type_id']));
        $parentType=pdo_get('cqkundian_farm_problem_type',array('uniacid'=>$uniacid,'id'=>$childType['pid']));
    }
    include $this->template('web/problem/problem_list_edit');
}

//常见问题保存
if($op=='problem_list_edit_save'){
    $data=array(
        'type_id'=>$_GPC['type_id'],
        'problem_title'=>$_GPC['problem_title'],
        'problem_value'=>$_GPC['problem_value'],
        'rank'=>$_GPC['rank'],
        'status'=>$_GPC['status'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
    );
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_problem',$data);
    }else{
        $res=pdo_update('cqkundian_farm_problem',$data,array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    if($res){
        message('操作成功',$this->createWebUrl('problem'));die;
    }else{
        message('操作失败');die;
    }
}

//常见问题状态修改
if($op=='problem_list_change_status'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_problem',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}
//删除问题
if($op=='problem_list_delete') {
    $id = $_GPC['id'];
    $res = pdo_delete('cqkundian_farm_problem',array('id'=>$id,'uniacid'=>$uniacid));
    echo  $res ?  json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//常见问题分类列表
if($op=='problem_type'){
    $condition=array(
        'uniacid'=>$uniacid,
        'pid'=>0,
    );
    $list=pdo_getall('cqkundian_farm_problem_type',$condition,'','','rank asc');
    for ($i=0;$i<count($list);$i++){
        $childData=pdo_getall('cqkundian_farm_problem_type',array('pid'=>$list[$i]['id'],'uniacid'=>$uniacid));
        $list[$i]['childData']=$childData;
    }
    include $this->template('web/problem/problem_type');
}

//常见问题编辑
if($op=='problem_type_edit'){
    //查询顶级分类
    $typeData=pdo_getall('cqkundian_farm_problem_type',array('uniacid'=>$uniacid,'pid'=>0));
    if(!empty($_GPC['id'])) {
        $list = pdo_get('cqkundian_farm_problem_type', array('id' => $_GPC['id'], 'uniacid' => $uniacid));
    }
    include $this->template('web/problem/problem_type_edit');
}
//常见问题保存
if($op=='problem_type_edit_save'){
    $data=array(
        'type_name'=>$_GPC['type_name'],
        'rank'=>$_GPC['rank'],
        'status'=>$_GPC['status'],
        'pid'=>$_GPC['pid'],
        'uniacid'=>$uniacid,
    );
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_problem_type',$data);
    }else{
        $res=pdo_update('cqkundian_farm_problem_type',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',url('site/entry/problem',array('m'=>'kundian_farm','op'=>'problem_type')));die;
    }else{
        message('操作失败');die;
    }
}

//问题分类改变状态
if($op=='problem_type_change_status'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_problem_type',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//删除问题分类
if($op=='problem_type_delete') {
    $id = $_GPC['id'];
    $res = pdo_delete('cqkundian_farm_problem_type',array('id'=>$id,'uniacid'=>$uniacid));
    echo  $res ?  json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//ajax获取分类信息
if($op=='getTypeData'){
    $pid=$_GPC['pid'];
    $typeData=pdo_getall('cqkundian_farm_problem_type',array('uniacid'=>$uniacid,'pid'=>$pid));
    echo json_encode(array('typeData'=>$typeData));die;
}

//常见问题设置
if($op=='problem_set'){
    $condition=array(
        'ikey'=>array('is_open_problem','problem_icon','is_open_service','service_icon'),
        'uniacid'=>$uniacid,
    );
    $btmData=pdo_getall('cqkundian_farm_set',$condition);
    $list=array();
    foreach ($btmData as $key=>$v){
        $list[$v['ikey']]=$v['value'];
    }
    include $this->template('web/problem/problem_set');
}

//保存常见问题设置
if($op=='problem_set_save'){
    $data=$_POST;
    $res=0;
    if(empty($data['is_open_problem'])){
        $data['is_open_problem']=0;
    }
    if(empty($data['is_open_service'])){
        $data['is_open_service']=0;
    }

    $data['problem_icon']=tomedia($data['problem_icon']);
    $data['service_icon']=tomedia($data['service_icon']);
    foreach ($data as $key=>$v){
        $updateData=array(
            'ikey'=>$key,
            'value'=>$v,
            'uniacid'=>$uniacid,
        );
        $cond=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $farmData=pdo_get('cqkundian_farm_set',$cond);
        if(empty($farmData)){
            $res+=pdo_insert('cqkundian_farm_set',$updateData);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$updateData,$cond);
        }
    }
    if($res){
        message('操作成功',url('site/entry/problem',array('m'=>'kundian_farm','op'=>'problem_set')));die;
    }else{
        message('操作失败');
    }
}




